#include "stdafx.h"
#include "PlayerAvatar.h"
#include <time.h>
#include <Windows.h>
#include "BoxComponent.h"

#include <sstream>

PlayerAvatar::PlayerAvatar(Ogre::Vector3 position) {
	m_node = NULL;
	m_pitchNode = NULL;
	m_health = 100;
	m_fireIntensity = 0;
	m_syncSound = new Sound2d(EVENTID_FMODPROJ_MUSIC_FLOURISH);
	m_apComp = NULL;
	m_entity = NULL;
	m_justFragged = false;
	//comment to disable physics
	
	m_apComp = new AvatarPhysicsComponent(position);
	m_physicsComponent = m_apComp;
}

PlayerAvatar::~PlayerAvatar() {
	delete m_syncSound;
	m_syncSound = NULL;
}

void PlayerAvatar::updateCharacter(float timestep, float dX, float dY, bool wantJump) {
	if(m_apComp) {
		//TODO change dx dy or change forward vector
		m_apComp->updateCharacter( timestep, dX, dY, wantJump );
	}
}

void PlayerAvatar::fire() {
	m_fireIntensity = 10;
	Ogre::Vector3 pos = m_node->getPosition();
	SoundPlayer::getInstance()->activateEvent3d(EVENTID_FMODPROJ_SFX_GUNSHOT, pos.x, pos.y, pos.z);
	m_justFragged = true;
}

float PlayerAvatar::getFireIntensity() {
	return m_fireIntensity;
}

void PlayerAvatar::setHealth(unsigned int health) {
	m_health = health;
}

void PlayerAvatar::resetFireIntensity() {
	m_fireIntensity = 0;
}

void PlayerAvatar::whenCameraAttached(){
	if(m_entity){

		m_entity->setVisible(false);
	}
}

void PlayerAvatar::whenCameraDetached(){
	if(m_entity){

		m_entity->setVisible(true);
	}
}

void PlayerAvatar::turn(float amount) {
	if(m_node) {
		m_node->yaw(Ogre::Radian((amount*3.14f)/180));
	}
	if(m_apComp) {
		m_apComp->turn( (amount*3.14f)/180 );
	}
}

void PlayerAvatar::setSceneNode(Ogre::SceneNode *node) {
	m_node = node;
	m_pitchNode = m_node->createChildSceneNode();
	m_pitchNode->translate(0,2,0);
}

void PlayerAvatar::jump() {
	if(m_physicsComponent) {
		m_physicsComponent->applyImpulse(Ogre::Vector3(0,10,0));
	}
}

void PlayerAvatar::updateLogic() {

	Ogre::Vector3 position = m_node->getPosition() + m_pitchNode->getPosition();
	
	Ogre::Vector3 vectorForward = m_node->getOrientation().zAxis();
	vectorForward.normalise();

	Ogre::Vector3  vectorUp = m_node->getOrientation().yAxis();
    vectorUp.normalise();

	m_justFragged = false;
}

void PlayerAvatar::createMovableObject(Ogre::SceneManager* sceneMgr, unsigned int id) {
	std::string idString;
	std::stringstream out;
	out << "Entity" << id;
	idString = out.str();

	m_entity = sceneMgr->createEntity(idString, "penguin.mesh");
	
	this->addMovableObject(m_entity);
	m_node->attachObject(m_entity);
	m_node->scale(Ogre::Vector3(1,1,1)/20);

	std::string idStringFish;
	std::stringstream out2;
	out2 << "EntityFish" << id;
	idStringFish = out2.str();

	Ogre::Entity* fish = sceneMgr->createEntity(idStringFish, "fish.mesh");
	Ogre::SceneNode* fishNode = m_pitchNode->createChildSceneNode();
	fishNode->attachObject(fish);
	fishNode->yaw(Ogre::Angle(110));
	fishNode->pitch(Ogre::Angle(30));
	fishNode->translate(-0.4f,-0.1f,0);
	fishNode->scale(Ogre::Vector3(1,1,1)*0.2);

}

void PlayerAvatar::damage(unsigned int damage) {
	if(m_health < damage) {
		m_health = 0;
	} else {
		m_health -= damage;
	}
}


unsigned int PlayerAvatar::getHealth() {
	return m_health;
}

bool PlayerAvatar::hasJustFragged() {
	return m_justFragged;
}

void PlayerAvatar::pitchCamera(float amount) {
	if(m_pitchNode) {
		 m_pitchNode->pitch(Ogre::Degree(amount));
		 // Angle of rotation around the X-axis.
		 float pitchAngle = (2 * Ogre::Degree(Ogre::Math::ACos(this->m_pitchNode->getOrientation().w)).valueDegrees());
 
		 // Just to determine the sign of the angle we pick up above, the
		 // value itself does not interest us.
		 float pitchAngleSign = this->m_pitchNode->getOrientation().x;
		 // Limit the pitch between -90 degress and +90 degrees, Quake3-style.
		 if (pitchAngle > 90.0f) {
			 if (pitchAngleSign > 0) {
				 // Set orientation to 90 degrees on X-axis.
				 this->m_pitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f), Ogre::Math::Sqrt(0.5f), 0, 0));
			 } else if (pitchAngleSign < 0) {
				 // Sets orientation to -90 degrees on X-axis.
				 this->m_pitchNode->setOrientation(Ogre::Quaternion(Ogre::Math::Sqrt(0.5f),
																		-Ogre::Math::Sqrt(0.5f), 0, 0));
			 }
		 }
	}
}

void PlayerAvatar::setPitchNode(Ogre::SceneNode *pitchNode) {
	m_pitchNode = pitchNode;
}

Ogre::SceneNode* PlayerAvatar::getCameraNode() {
	return m_pitchNode;
}

bool PlayerAvatar::isAlive() {
	return m_health > 0;
}